package o2o.web.actions.order.thread;

import com.jinyou.utils.common.DateUtils;
import o2o.cache.order.OrderInfoMemory;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.PO.order.ORDER_TYPE;
import o2o.dao.PO.order.OrderInfo;
import o2o.web.actions.order.OrderMan;
import o2o.system.data.SYS_SETTINGS;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//商家超时未接单  自动取消订单
public class CancelOrder4ShopThread extends Thread {
    public static Map<String, Long> map = new ConcurrentHashMap<>();   //检测订单一定时间内未支付  直接取消

    private boolean threadFlag = true;

    private static CancelOrder4ShopThread thread = new CancelOrder4ShopThread();

    public static CancelOrder4ShopThread getInstance() {
        if (thread == null) {
            thread = new CancelOrder4ShopThread();
        }
        return thread;
    }

    private CancelOrder4ShopThread() {
    }


    @Override
    public void run() {
        if (null != SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME && SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME > 0) {
            while (threadFlag) {
                try {
                    Thread.sleep(120 * 1000);  //线程每次停止120秒  zhuwx
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (map.size() > 0) {
                    synchronized (map) {
                        //检测是否已支付成功
                        for (Map.Entry<String, Long> data : map.entrySet()) {
                            String key = data.getKey();
                            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(key);
                            if (null == orderInfo) {
                                map.remove(key);
                                continue;
                            }

                            if (0 != orderInfo.getOrderStatus() - ORDER_STATUS.PAYED) {
                                map.remove(key);
                                continue;
                            }

                            //外卖订单
                            if (null != orderInfo.getOrderType() && (0 != ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()
                                    && 0 != ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
                                    && 0 != ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
                                    && 0 != ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
                                    && 0 != ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())
                            ) {
                                map.remove(key);
                                continue;
                            }

                            if (null == orderInfo.getPayTime()) {
                                continue;
                            }

                            if (DateUtils.getCurrTime() - orderInfo.getPayTime() > SYS_SETTINGS.ORDER_AUTO_CANCEL_TIME * 60 * 1000) {
                                //订单创建时间时间错过 x分钟才去检测
                            } else {
                                continue;
                            }

                            //取消订单
                            ResultInfo resultInfo = OrderMan.cancelOrder(null, orderInfo, "admin", "超时，商家未接单", true);
                            if (null != resultInfo && resultInfo.isSuccess()) {
                                //成功了
                                map.remove(key);
                                continue;
                            }
                        }
                    }
                }


            }
        }
    }

}
